﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;


namespace mojoPortal.Web.RS.SBM.AppCode.Lib
{
    public class RSSBMDataLookupLoader : mojoPortal.Web.RS.AppCode.Lib.RSDataLookupLoader
    {
        public static void LoadInventoryCategoryDDL(int companyid, DropDownList ddlInventoryCategory, string itemIndexZeroText = "All", string itemIndexZeroValue = "0")
        {
            SqlCommand myCommand;
            SqlDataReader myDataReader;
            String sqlSELECT = "SELECT ProductId,ProductName FROM rs_tblInventory WHERE CompanyId=@CompanyId AND IsEquipment=0 AND (ParentId IS NULL OR ParentId=0) ORDER BY ProductName ASC";
            SqlParameter param;
            SqlConnection myConnection;

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[RSSBMCommonFunctions.GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);
            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            ddlInventoryCategory.Items.Clear();
            ddlInventoryCategory.DataSource = myDataReader;
            ddlInventoryCategory.DataTextField = "ProductName";
            ddlInventoryCategory.DataValueField = "ProductId";
            ddlInventoryCategory.DataBind();
            ddlInventoryCategory.Items.Insert(0, new ListItem(itemIndexZeroText, itemIndexZeroValue));
            myConnection.Close();
        }

        public static void LoadAssignToCHK(int companyid, CheckBoxList chkAssignTo)
        {
            SqlCommand myCommand;
            SqlDataReader myDataReader;
            String sqlSELECT = "SELECT e.EmployeeId, e.FirstName + ' ' + e.LastName AS FullName, e.CompanyId " +
                                "FROM rs_tblGroup g " +
                                "inner join rs_tblEmployee e " +
                                "on g.GroupId=e.GroupId " +
                                "WHERE e.CompanyId=@companyid AND " +
                                "g.GroupName IN ('technician', 'manager') AND " +
                                "g.CompanyId=@companyid ORDER BY e.FirstName ASC";
            SqlParameter param = new SqlParameter();
            SqlConnection myConnection;

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[RSSBMCommonFunctions.GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);
            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            chkAssignTo.Items.Clear();
            chkAssignTo.DataSource = myDataReader;
            chkAssignTo.DataTextField = "FullName";
            chkAssignTo.DataValueField = "EmployeeID";
            chkAssignTo.DataBind();
            myConnection.Close();
        }

        
    }
}